﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Animation;
using Microsoft.Xna.Framework;
using AI;

namespace MapLibrary.Utility
{
    public class SpritePool<T> : Pool<T> where T : Sprite, new()
    {
        private GameWorld world;
        private SpriteType type;

        public SpritePool(GameWorld world, SpriteType type)
        {
            this.world = world;
            this.type = type;

            Initialize();
        }

        protected override T Construct()
        {
            Sprite toEdit = new T();

            toEdit.Scale = new Vector2(1.0f, 1.0f);
            toEdit.SpriteTypeName = type.Name;
            toEdit.Type = type.Clone();
            toEdit.Type.CurrentState = type.StateList.ElementAt(0).StateName;

            Bot b = BotFactory.CreateBot(type.BotName);
            if (b != null)
            {
                b.Owner = toEdit;
                toEdit.Bot = b;
            }

            toEdit.Update();

            return (T)toEdit;
        }

        protected override void PostNew(T toEdit)
        {
            toEdit.RegisterToWorld(world);
            toEdit.Rotation = 0.0f;
        }

        protected override void PostReclaim(T toEdit)
        {
            world.CurrentLevel.AddBodyToRemove(toEdit);
        }
    }
}
